﻿using AutoMapper;
using Hotel.Common;
using Hotel.Domain;
using Hotel.IRepository;
using Hotel.IServices;
using Hotel.IServices.RequestDTO;
using Hotel.IServices.ResponseDTO;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Hotel.Services
{
    public class GoodsServices: IGoodsServices
    {
        
        private readonly IMapper mapper;  //automapper 迁移
        private readonly ILogger<GoodsServices> logger; //日志
        private readonly IBaseRepository<Googds> goodsRepository;  //商品仓储

        public GoodsServices(IMapper mapper, ILogger<GoodsServices> logger, IBaseRepository<Googds> goodsRepository)
        {
            this.mapper = mapper;
            this.logger = logger;
            this.goodsRepository = goodsRepository;
        }

        public async Task<ApiResult> AddGoodsAsync(AddGoodsAsyncReDto dto)
        {
            try
            {
                //声明返回值
              var result=  new ApiResult("失败");

                //转换Dto
                var entity = mapper.Map<Googds>(dto);
                //插入数据
             var res= await goodsRepository.InsertDataAsync(entity);
                if (res)
                {
                    result.Success();
                }

                return result;
            }
            catch (Exception)
            {

                throw;
            }
        }


        /// <summary>
        /// 分页查询商品列表
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        public async Task<ApiResult<PagingResult<IList<QueryGoodsListPagedAsyncRespDto>>>> QueryGoodsListPagedAsync(QueryGoodsListPagedAsyncReqDto dto)
        {
            try
            {
                var result = new ApiResult<PagingResult<IList<QueryGoodsListPagedAsyncRespDto>>>("失败");

                //获取所有数据
                var query = await goodsRepository.GetAllAsync(p=>p.IsDel==0);

                //根据商品名称查询
                if (!string.IsNullOrEmpty(dto.GoodsName))
                {
                 query=   query.Where(p => p.GoodsName.Contains(dto.GoodsName)).ToList();
                }

                //获取总条数
                var TotalCount=query.Count();

                //获取总页数
                var TotalPage = (int)Math.Ceiling(TotalCount * 1.0 / dto.PageSize);
                //分页
                query = query.OrderBy(p => p.GoodsId).Skip((dto.PageIndex - 1) * dto.PageSize).Take(dto.PageSize).ToList();

                //转换Dto
                var data = mapper.Map<IList<QueryGoodsListPagedAsyncRespDto>>(query);

                //返回标准的分页返回值
            var page=    new PagingResult<IList<QueryGoodsListPagedAsyncRespDto>>();
                page.totalCount= TotalCount;
                page.totalPage= TotalPage;
                page.data= data;

                result.Success(page);
                return result;
            }
            catch (Exception)
            {

                throw;
            }
        }


        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="goodid"></param>
        /// <returns></returns>
        public async Task<ApiResult> DelGoodsAsync(long goodid)
        {
                  var result= new ApiResult("删除失败");

            //获取商品信息
                 var info=   await goodsRepository.GetAsync(p => p.GoodsId == goodid);
            if (info!=null &&info.GoodsId>0) {

                //软删除  isdel=1:删除  =0:未删除
                info.IsDel = 1;
                //数据删除后更新
               var res= await goodsRepository.UpdateDataAsync(info);
                if(res) result.Success();
            }
            return result;
        }


        /// <summary>
        /// 根据id查询商品信息
        /// </summary>
        /// <param name="goodid"></param>
        /// <returns></returns>
        public async Task<ApiResult> UpSqlUser(long goodid)
        {
            try
            {
                var result = new ApiResult<Googds>("失败");
             var info=   await goodsRepository.GetAsync(p => p.GoodsId == goodid);
               
                   result.Success(info);
                
                return result;
            }
            catch (Exception)
            {

                throw;
            }

        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        public async Task<ApiResult> UpUser(GetUpGoodsReDto dto)
        {

            var result=new ApiResult("修改失败");
            //获取商品信息
          var info=  await goodsRepository.GetAsync(p => p.GoodsId == dto.GoodsId);
            if (info!=null&&info.GoodsId>0)
            {
                //将dto数据转换为实体类数据
               var data= mapper.Map(dto, info);

                //数据更新
                var query=await goodsRepository.UpdateDataAsync(data);
                if (query) {
                  result.Success();
                }

            }
            return result;
        }
    }
}
